import {
  MessageBox,
  Message,
} from 'element-ui';
import Store from '../store';
import axios from 'axios'
export default {
  methods: {
    showConfirm(title, callbackFunction, name) {
      MessageBox.confirm(title, name ? name : '提示' , {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        callback: action => {
          if (action == 'cancel') {
            Message({
              type: 'info',
              message: `已取消该操作`
            });
          } else {
            callbackFunction()
          }
        }
      })
    },
    auditConfirm(flag, callbackFunction, value){
        let p
        if (flag === 1) {
          p = MessageBox.confirm('确定要通过吗？')
        } else {
          p = MessageBox.prompt('请输入驳回理由', {
            inputPattern: /\S+/,
            inputValue: value ? value : '',
            inputErrorMessage: '请输入驳回理由'
          })
        }
        p.then(res => {
          callbackFunction(res)
        })
    },
    // 下载文档流文件
    downloadBlob(params, url, fileName) {
      axios({
        method: 'get',
        url: url,
        params: params,
        responseType: 'blob',
      }).then(response => {
        // 兼容ie
        if (window.navigator.msSaveBlob) {
          try {
            const blobObject = new Blob([response.data])
            //设置文件名称为后台返回名称
            window.navigator.msSaveBlob(blobObject, fileName);
          } catch (e) {
            console.log(e)
          }
        } else {
          // 其他浏览器
          // eslint-disable-next-line no-undef
          const blob = new Blob([response.data], {
            type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8'
          }) // application/vnd.openxmlformats-officedocument.spreadsheetml.sheet这里表示xlsx类型
          const downloadElement = document.createElement('a')
          const href = window.URL.createObjectURL(blob) // 创建下载的链接
          downloadElement.href = href
          //设置文件名称为后台返回名称，也可以自定义
          downloadElement.download = fileName
          document.body.appendChild(downloadElement)
          downloadElement.click() // 点击下载
          document.body.removeChild(downloadElement) // 下载完成移除元素
          window.URL.revokeObjectURL(href) // 释放掉blob对象
          return response.data
        }
      })
    }
  }
}
